<?php
namespace app\common\model;

/**
 * 定义产品类型
 */
class ProductType
{
    const PRODUCTS_LIST = [
        [
            'product_id' => 1,
            'key'        => 'video',
            'title'      => '点播',
        ],
        [
            'product_id' => 2,
            'key'        => 'live',
            'title'      => '直播',
        ],
        [
            'product_id' => 3,
            'key'        => 'offline',
            'title'      => '面授',
        ],
        [
            'product_id' => 4,
            'key'        => 'classes',
            'title'      => '班级',
        ],
        [
            'product_id' => 5,
            'key'        => 'vip',
            'title'      => '会员',
        ],
        [
            'product_id' => 6,
            'key'        => 'balance',
            'title'      => '余额',
        ],
        [
            'product_id' => 7,
            'key'        => 'credit',
            'title'      => '积分',
        ],
        [
            'product_id' => 8,
            'key'        => 'exam',
            'title'      => '公开考试',
        ],
        [
            'product_id' => 9,
            'key'        => 'special',
            'title'      => '专项练习',
        ],
        [
            'product_id' => 10,
            'key'        => 'rollup',
            'title'      => '套卷练习',
        ],
        [
            'product_id' => 11,
            'key'        => 'goods',
            'title'      => '商品',
        ],
    ];

    /**
     * 根据产品key获取产品ID
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-30
     * @param    string                         $key [description]
     * @return   [type]                              [description]
     */
    public static function getProductIdByKey(string $key)
    {
        $data = array_column(ProductType::PRODUCTS_LIST, 'product_id', 'key');

        return $data[$key] ?? 0;
    }

    /**
     * 根据产品key获取产品ID字符串
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-30
     * @param    array                         $key [description]
     * @return   [type]                              [description]
     */
    public static function getProductIdStrByKeys(array $keys)
    {
        $data = array_column(ProductType::PRODUCTS_LIST, 'product_id', 'key');
        $ids  = '';

        foreach ($keys as $key) {
            $data[$key] && $ids .= $data[$key] . ',';
        }

        return rtrim($ids, ',');
    }

    /**
     * 根据产品ID获取产品key
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-30
     * @param    string                         $product_id [description]
     * @return   [type]                             [description]
     */
    public static function getProductKeyById(int $product_id)
    {
        $data = array_column(ProductType::PRODUCTS_LIST, 'key', 'product_id');

        return $data[$product_id] ?? '';
    }

    /**
     * 根据产品ID获取产品名称
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-30
     * @param    int                            $product_id [description]
     * @return   [type]                             [description]
     */
    public static function getProductTitleById(int $product_id)
    {
        $data = array_column(ProductType::PRODUCTS_LIST, 'title', 'product_id');

        return $data[$product_id] ?? '';
    }

    /**
     * 根据产品KEY获取产品名称
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-07-30
     * @param    string                         $key [description]
     * @return   [type]                              [description]
     */
    public static function getProductTitleByKey(string $key)
    {
        $data = array_column(ProductType::PRODUCTS_LIST, 'title', 'key');

        return $data[$key] ?? '';
    }

}
